//  MainViewController.m
//  maocuhuiOASystem
//
//  Created by michael on 16/3/20.
//  Copyright © 2016年 czd. All rights reserved.
//
#import "zhutiViewController.h"
#import "gouwuchelistViewController.h"
#import "webtViewController.h"
#import "shangpinlistViewController.h"
#import "zjyoupinViewController.h"
#import "caigoulistViewController.h"
#import "threelevelViewController.h"
#import "TableViewCellstylezero.h"
#import "TableViewCelltwo.h"
#import "TableViewCellstyle2.h"
#import "TableViewCellstyle1.h"
#import "TOPageTitleView.h"
#import "CycleCollectionViewfirst.h"
#import "toupiaoViewController.h"
#import "zhaozcViewController.h"
#import "rongziViewController.h"
#import "ZLCGuidePageView.h"
#import "titlexqViewController.h"
#import "picxqViewController.h"
#import "bendimoreViewController.h"
#import "bendipicTableViewCell.h"
#import "bendititleTableViewCell.h"
#import "zhaocanpinViewController.h"
#import "gwchaxunViewController.h"
#import "dzggViewController.h"
#import "fwclViewController.h"
#import "ViewController3.h"
#import "UIViewController+MMDrawerController.h"
#import "fwdjViewController.h"
#import "MainViewController.h"
#import "OALoginViewController.h"
#import "itemcell.h"
#import "OABaseNavViewController.h"

#import "MainViewCell.h"
#import "BaseModel.h"
#import "ReceieveViewController.h"
//#import "AuthHelper.h"
#define kWB_Login_NotificationName @"requestdata"
static NSString * identifier0 = @"cxCellID0";
static NSString * identifier2 = @"cxCellID2";

@interface HomeModel : BaseModel

@property(nonatomic,copy)NSString *  exchange;
@property(nonatomic,copy)NSString *  ksmail;
@property(nonatomic,copy)NSString *  mail;
@property(nonatomic,copy)NSString *  offiece;
@property(nonatomic,copy)NSString *  whetherExchange;
@property(nonatomic,copy)NSString *  whetherSms;
@property(nonatomic,copy)NSString * read;
@end
@implementation HomeModel


@end
@interface MainViewController ()<TOPageTitleViewDelegate>
@property (nonatomic,strong) UIButton *button1;
@property (nonatomic,strong) UIButton *button2;
@property(nonatomic,strong)NSMutableArray * contentArray;

//@property (nonatomic,strong) UICollectionView *collectionView;
@property(nonatomic,strong)HomeModel * homeModel;
@property(nonatomic,strong)NSMutableArray * originalData;
@property(nonatomic,strong)NSArray * array;
@property (nonatomic,strong) TOPageTitleView *titleView;
@property (nonatomic,strong) NSMutableArray<TOPageItem *> *titles;

//@property (nonatomic, strong) AuthHelper *helper;
@end

@implementation MainViewController
static NSString * const reuseIdentifier = @"CellIdentifier";
-(NSMutableArray*)contentArray{
    if (_contentArray == nil) {
        _contentArray = [NSMutableArray array];
    }
    return _contentArray;
}

- (void)viewDidLoad {

        self.currentPage = 1;
     [self   setCustomerTitle:NSLocalizedString(@"浙江省贸易促进网",nil)];
//self.title = @"浙江省贸易促进网";
    [super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];





            [self loadData]; //加载网络数据

[self upapp];


    NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:

[UIColor whiteColor],NSForegroundColorAttributeName, nil];
 
[self.navigationController.navigationBar setTitleTextAttributes:attributes];
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithHexString:@"0a62ea"];
   NSString *language = [[NSUserDefaults standardUserDefaults] objectForKey:@"language"];
if([language isEqualToString:@"0"]){
                          [[NSUserDefaults standardUserDefaults] setObject:@"1" forKey:@"website"];


}else{

           [[NSUserDefaults standardUserDefaults] setObject:@"2" forKey:@"website"];

}

  [self _backbutton];
   
   
// 判断版本

      //修改返回按钮的颜色和字体
  self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
//   UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStyleDone target:nil action:nil];
//    self.navigationItem.backBarButtonItem = barButtonItem;

   
}

//-(void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset{
//_tableView.frame = CGRectMake(0, 0, KScreenWidth, KScreenHeight-kTabBarHeight);
//    [self.navigationController setNavigationBarHidden:velocity.y>0 animated:YES];
//}



- (void)dealloc
{
    // 移除通知
    [[NSNotificationCenter defaultCenter] removeObserver:self name:kWB_Login_NotificationName object:nil];
    }
// 使用storyboard创建的控制调用的初始化方法
- (instancetype)initWithCoder:(NSCoder *)coder
{
    self = [super initWithCoder:coder];
    if (self) {
        // 注册通知
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(news) name:kWB_Login_NotificationName object:nil];
       
        
    }
    return self;
}
- (void)upapp{

NSDictionary *dic= [[NSUserDefaults standardUserDefaults] objectForKey:@"dic0"];
int i = [dic[@"type"] count];
NSLog(@"%@",dic);
     NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];

    NSString *appVersion = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
NSString *website = [[NSUserDefaults standardUserDefaults] objectForKey:@"website"];
   NSString *language = [[NSUserDefaults standardUserDefaults] objectForKey:@"language"];
NSString *bodyStr = [NSString stringWithFormat:@"<?xml version='1.0' encoding='UTF-8'?><message><header><userName>administrator</userName><password>b628e2c3b78ef9bf269c489b35176e48</password><website>%@</website><language>%@</language><type>%@</type></header><body><localVersion>%@</localVersion><type>%@</type><language>%@</language></body></message>",website,language,BAtype,appVersion,BAtype,language];
  bodyStr= [bodyStr URLEncodedString];
 NSArray * array = @[@{@"arg0":bodyStr}];
     NSString * path = [NSString stringWithFormat:@"%@%@",BAip,blogin];
  [HttpTools requestWithURL:path arrays:array method:@"isNewVersion" success:^(id JSON) {
  NSLog(@"%@",JSON);
//   [self.view makeToast:JSON[@"msg"]];
                  if([[NSString stringWithFormat:@"%@",JSON[@"code"]] isEqualToString:@"200"]){
                              UIAlertController *alert = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"更新", nil) message:NSLocalizedString(@"程序已经更新,再重新下载", nil) preferredStyle:UIAlertControllerStyleAlert];
             UIAlertAction *action = [UIAlertAction actionWithTitle:NSLocalizedString(@"更新", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

//                if (url.length != 0) {

                    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:JSON[@"appUrl"]]];

//                }

            }];

            [alert addAction:action];
            [self presentViewController:alert animated:YES completion:nil];
                  }
    } failure:^(NSError *error) {
      [MBProgressHUD hideHUDForView:self.view animated:YES];

    }];

//[httpTool ZBPostNetDataWith:url withDic:nil andSuccess:^(NSDictionary* dictionary) {
//if([[NSString stringWithFormat:@"%@",dictionary[@"success"]] isEqualToString:@"true"]){
//
//            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"更新" message:@"程序已经更新,再重新下载" preferredStyle:UIAlertControllerStyleAlert];
//             UIAlertAction *action = [UIAlertAction actionWithTitle:@"更新" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//                
//                if (url.length != 0) {
//                    
//                    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:dictionary[@"appurl"]]];
//
//                }
//                
//            }];
//    
//            [alert addAction:action];
//            [self presentViewController:alert animated:YES completion:nil];
//
//
//}
////    NSArray *array = dictionary;
//    } andFailure:^{
//
//    }];


}
- (void)viewWillAppear:(BOOL)animated{

}

- (void)loadData{
[_button removeFromSuperview];
 [MBProgressHUD showHUDAddedTo:self.view animated:YES];

//&numPerPage=5
//NSString *url = [[NSString alloc]initWithFormat:@"%@%@&numPerPage=5",kbendimore,_id];
    NSMutableDictionary *dics=[NSMutableDictionary dictionary];
      dics[@"userName"] = @"administrator";
    dics[@"password"] = @"b628e2c3b78ef9bf269c489b35176e48";
          dics[@"website"] = [[NSUserDefaults standardUserDefaults] objectForKey:@"website"];
    dics[@"language"] = [[NSUserDefaults standardUserDefaults] objectForKey:@"language"];
        dics[@"type"] = BAtype;
NSString *dicstring = [httpTool dictionaryToJson:dics];
//dicstring= [dicstring URLDecodedString];
dicstring= [dicstring URLEncodedString];
NSString *url = [[NSString alloc]initWithFormat:@"%@/api/homepage/index?data=%@",BAip,dicstring];

 [httpTool ZBPostNetDataWith:url withDic:nil andSuccess:^(NSDictionary* dictionary){
  [MBProgressHUD hideHUDForView:self.view animated:YES];
_dic =  dictionary;
NSLog(@"%@",dictionary);
if ([[NSString stringWithFormat:@"%@", dictionary[@"code"]]isEqualToString:@"200"]) {
//新闻轮播
          _articlelistarray = [NSMutableArray array];
          NSArray *array = dictionary[@"articleList"];
for (NSDictionary *dic in array) {
    [_articlelistarray addObject: dic];
}
_imagearray = [NSMutableArray array];
_titlearray = [NSMutableArray array];
for (NSDictionary *dic in _articlelistarray) {
    NSString *url = [NSString stringWithFormat:@"%@",dic[@"defaultPicUrl"]];
    [_imagearray addObject: url];
}
if(_articlelistarray){
for (NSDictionary *dic in _articlelistarray) {
    [_titlearray addObject: dic[@"title"]];
}
}
//主推商品
_allMainPushProductListarray = dictionary[@"allMainPushProductList"];
_oneLevelCategoryListarray = dictionary[@"oneLevelCategoryList"];

[self initview];
[self.tableView headerEndRefreshing];
}else{
 [MBProgressHUD hideHUDForView:self.view animated:YES];

            [self.view makeToast:@"暂时还没数据哦"];

}

 }andFailure:^{
 [self initview2];
  [MBProgressHUD hideHUDForView:self.view animated:YES];

         [self.tableView headerEndRefreshing];

 }];

}
- (void)initview2{
if(!_button){
   _button = [UIButton buttonWithType:UIButtonTypeCustom];
    _button.frame = CGRectMake(KScreenWidth/2-50, 300, 100,50);
    [_button setTitle:NSLocalizedString(@"点击重试", nil) forState:UIControlStateNormal];
    [_button setTitleColor:[UIColor blackColor]forState:UIControlStateNormal];
        [_button addTarget:self action:@selector(loadData) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:_button];

}
}
- (void)initview{
if(!_tableView){
   _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 40, KScreenWidth, KScreenHeight-kTopHeight-kTabBarHeight-40) style:UITableViewStylePlain];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableView.dataSource = self;
_tableView.delegate = self;
_tableView.backgroundView.backgroundColor = [UIColor colorWithHexString:@"f0eff5"];
 [_tableView registerNib:[UINib nibWithNibName:@"TableViewCellstyle1" bundle:nil] forCellReuseIdentifier:identifier0];
  [_tableView registerNib:[UINib nibWithNibName:@"TableViewCelltwo" bundle:nil] forCellReuseIdentifier:identifier2];

[self.view addSubview:_tableView];
}
 __weak typeof(self) weakSelf = self;
[self.tableView addHeaderWithCallback:^{
        weakSelf.currentPage = 1;
        [weakSelf loadData]; //加载网络数据
    }];
//    [self.tableView headerBeginRefreshing];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
      switch (section) {
        case 0:
            return 1;
            break;
        case 1:
        if(_allMainPushProductListarray != nil && ![_allMainPushProductListarray isKindOfClass:[NSNull class]] && _allMainPushProductListarray.count != 0){
        
                    return 1;
        }else{
                    return 0;
        }
        case 2:
            return _oneLevelCategoryListarray.count;
//            return 0;

        default:
            return 1;
            break;
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
if(indexPath.section ==0){

return 250+10+10+20;


}
else if (indexPath.section ==1){

return (KScreenWidth-30)/4+30+10+10;
}
else if (indexPath.section ==2){
NSDictionary *dic =_oneLevelCategoryListarray[indexPath.row];

if ([[NSString stringWithFormat:@"%@", dic[@"sort"]]isEqualToString:@"1"]) {
    return (KScreenWidth-30)/4+150+40+40+10;

}else if ([[NSString stringWithFormat:@"%@", dic[@"sort"]]isEqualToString:@"0"]) {
    return ((KScreenWidth-40-1)/2+60)*3+40+2+10;

}


else{

    return 40+40+100+150+10;


}
}

return 1;

}
#pragma mark UITableView dataScource
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
if(indexPath.section ==0){
if(indexPath.row ==0){
 static NSString *CellIdentifier0 = @"Cellbendititlepic0";

    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier0];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
if (cell==nil) {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier0];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;

    }
      _topPhotoBoworr = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(5, 5, Screen_Width-10, 200-10) delegate:self placeholderImage:nil];
        _topPhotoBoworr.boworrWidth = Screen_Width ;
        _topPhotoBoworr.pageControlAliment = SDCycleScrollViewPageContolAlimentRight;
        _topPhotoBoworr.bannerImageViewContentMode = UIViewContentModeScaleAspectFill;
        _topPhotoBoworr.cellSpace = 10;
               _topPhotoBoworr.pageControlAliment = SDCycleScrollViewPageContolAlimentCenter;
        _topPhotoBoworr.currentPageDotColor = [UIColor orangeColor];
        _topPhotoBoworr.pageDotColor = [UIColor whiteColor];
        _topPhotoBoworr.titleLabelHeight = 30;
      _topPhotoBoworr.imageURLStringsGroup = [_imagearray mutableCopy];
//        _topPhotoBoworr.titlesGroup = [_titlearray mutableCopy];
        [cell.contentView addSubview:self.topPhotoBoworr];
    NSArray *midarray = @[@"全部分类",@"浙江优品",@"采购信息",@"主题馆"];
    NSString *string =NSLocalizedString(@"全部分类", nil);
    NSArray *titlearray = @[NSLocalizedString(@"全部分类", nil),NSLocalizedString(@"浙江优品", nil),NSLocalizedString(@"采购信息", nil),NSLocalizedString(@"主题馆", nil)];
for (int i= 0; i<4;i++) {
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.tag = 200+i;
    button.frame = CGRectMake(i*(KScreenWidth/4), _topPhotoBoworr.bottom+10, KScreenWidth/4,50+20);
    [button setTitle:titlearray[i] forState:UIControlStateNormal];
    [button setTitleColor:[UIColor blackColor]forState:UIControlStateNormal];
    [button setImage:[UIImage imageNamed:midarray[i]] forState:UIControlStateNormal];
    button.titleLabel.numberOfLines = 0;
    button.titleLabel.font = [UIFont systemFontOfSize: 13.0];
        [button layoutButtonWithEdgeInsetsStyle:MKButtonEdgeInsetsStyleTop
                                    imageTitleSpace:20];
    [button addTarget:self action:@selector(midaction:) forControlEvents:UIControlEventTouchUpInside];
    
    [cell.contentView addSubview:button];
    
    
}
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, _topPhotoBoworr.bottom +70+20, KScreenWidth, 10)];
    view.backgroundColor = [UIColor colorWithHexString:@"f0eff5"];
    [cell.contentView addSubview:view];
    return cell;

}else{
return nil;

}
}else if(indexPath.section ==1){
if (indexPath.row ==0) {
//    static NSString *CellIdentifier1 = @"Cellbendititlepic1";
//
//    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
//if (cell==nil) {
//        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier1];
//    }
//     UIView *view = [[UIView alloc]initWithFrame:CGRectMake(5, 10, 10, 20)];
//    view.backgroundColor = [UIColor colorWithHexString:@"0a62ea"];
////    view.backgroundColor = [UIColor redColor];
//
//    [cell.contentView addSubview:view];
//    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(view.right+10,10, 60, 20)];
//    label.text  = NSLocalizedString(@"主推商品", nil);
//    label.textColor = [UIColor colorWithHexString:@"0a62ea"];
//   label.font = [UIFont boldSystemFontOfSize:14];
//   [cell.contentView addSubview:label];
//   UIImageView *image = [[UIImageView alloc]initWithFrame:CGRectMake(label.right+10, 10, 50, 20)];
//   image.image = [UIImage imageNamed:NSLocalizedString(@"每日更新", nil)];
//       [cell.contentView addSubview:image];
//    CycleCollectionViewfirst *cyleView = [[CycleCollectionViewfirst alloc] initWithFrame:CGRectMake(0, 40, KScreenWidth,(KScreenWidth-30)/4)];
//      [cell.contentView addSubview:cyleView];
//         cyleView.data = _allMainPushProductListarray;
//         [cyleView.collectionView reloadData];
//UIView *view2 = [[UIView alloc]initWithFrame:CGRectMake(0, cyleView.bottom, KScreenWidth, 10)];
//    view2.backgroundColor = [UIColor colorWithHexString:@"f0eff5"];
//    [cell.contentView addSubview:view2];
//    return cell;
  static NSString *CellIdentifier1 = @"CellIdentifier1";

    TableViewCellstyle2 *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
if (cell==nil) {

        cell=[[TableViewCellstyle2 alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier1];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;

    }

    cell.array = _allMainPushProductListarray;
    return cell;

}
else{
return nil;
}
}
else if(indexPath.section ==2){


NSDictionary *dic =_oneLevelCategoryListarray[indexPath.row];

if ([[NSString stringWithFormat:@"%@", dic[@"sort"]]isEqualToString:@"1"]) {
    TableViewCellstyle1 * cell = [tableView dequeueReusableCellWithIdentifier:identifier0];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell = [[[UINib nibWithNibName:@"TableViewCellstyle1" bundle:nil]instantiateWithOwner:self options:nil]lastObject];
    cell.dict =dic;
    return cell;

}else if ([[NSString stringWithFormat:@"%@", dic[@"sort"]]isEqualToString:@"0"]) {
  static NSString *CellIdentifierzero = @"CellIdentifierzero";

    TableViewCellstylezero *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifierzero];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
 
if (cell==nil) {
cell.selectionStyle = UITableViewCellSelectionStyleNone;

        cell=[[TableViewCellstylezero alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifierzero];
    }
cell.backgroundColor = [UIColor colorWithHexString:@"f0eff5"];
    cell.dic =dic;
    return cell;


}

else{

   TableViewCelltwo * cell = [tableView dequeueReusableCellWithIdentifier:identifier0];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell = [[[UINib nibWithNibName:@"TableViewCelltwo" bundle:nil]instantiateWithOwner:self options:nil]lastObject];
    cell.dict =dic;
    return cell;

}
  
}

else{
     static NSString *CellIdentifier2 = @"Cellbendititlepic2";

    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier2];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
if (cell==nil) {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier2];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;

    }
     UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
      button.frame = CGRectMake((KScreenWidth-200)/2, 10, 200, 40);
//          button.backgroundColor = [UIColor blueColor];

//        [button.layer setBorderColor:[UIColor grayColor].CGColor];
//    [button.layer setBorderWidth:1];
// [button.layer setMasksToBounds:YES];
    [button setTitle:@"加载更多" forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont systemFontOfSize: 15.0];
    [button setTitleColor:[UIColor grayColor]forState:UIControlStateNormal];
        button.layer.cornerRadius = button.height/2;
                [button addTarget:self action:@selector(action) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:button];
    return cell;
    
  
    }

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

NSLog(@"222");

}
- (void)_backbutton{
//     UIBarButtonItem * rightItem  = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"友好合作平台"] style:UIBarButtonItemStylePlain target:self action:@selector(createNewSMSAction)];
//
//                     self.navigationItem.rightBarButtonItem = rightItem;
//
 UIBarButtonItem * rightItem1  = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"logo"] style:UIBarButtonItemStylePlain target:self action:nil];
 UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame= CGRectMake(0, 0, 40, 44);
[btn setImage:[UIImage imageNamed:@"logo"] forState:UIControlStateNormal];
UIBarButtonItem *btn_right = [[UIBarButtonItem alloc] initWithCustomView:btn];

UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc]
                                   initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
                                   target:nil action:nil];
                                   negativeSpacer.width = 40;
self.navigationItem.leftBarButtonItems = [NSArray arrayWithObjects:negativeSpacer, btn_right, nil];

//                     self.navigationItem.leftBarButtonItem = rightItem1;

   

    
    _topview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, 40)];
    _topview.backgroundColor = [UIColor colorWithHexString:@"0a62ea"];
    [_topview addSubview:self.searchBar];
    [self.view addSubview:_topview];
    UIView *cenview = [[UIView alloc]initWithFrame:CGRectMake(KScreenWidth/2-100, 0, 200, 40)];
//        [_topview addSubview:cenview];
UIButton *left = [UIButton buttonWithType:UIButtonTypeCustom];
left.frame = CGRectMake(0, 0, 40, 40);
[left addTarget:self action:@selector(createNewSMSAction2) forControlEvents:UIControlEventTouchUpInside];
[left setImage:[UIImage imageNamed:@"购物车"] forState:UIControlStateNormal];
[_topview addSubview:left];

UIButton *right = [UIButton buttonWithType:UIButtonTypeCustom];
right.frame = CGRectMake(KScreenWidth-40, 0, 40, 40);
[right addTarget:self action:@selector(createNewSMSAction) forControlEvents:UIControlEventTouchUpInside];
[right setImage:[UIImage imageNamed:@"友好合作平台"] forState:UIControlStateNormal];
[_topview addSubview:right];
}
- (void)createNewSMSAction{

webtViewController * vc = [[webtViewController alloc]init];
vc.hidesBottomBarWhenPushed = YES;
vc.url = _dic[@"fdshipurl"];
[self.navigationController pushViewController:vc animated:YES];
}
- (void)createNewSMSAction2{
   UIStoryboard * storyBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];

 if (![[[NSUserDefaults standardUserDefaults]objectForKey:@"autoLogin"]isEqualToString:@"Y"]&&![[DanLi sharedDanLi].stay isEqualToString:@"yes"]) {
      OALoginViewController * loginVC = [storyBoard instantiateViewControllerWithIdentifier:@"LoginVC"];
        UINavigationController * navVC = [[UINavigationController alloc]initWithRootViewController:loginVC];
        loginVC.navigationController.navigationBar.hidden = YES;
                 self.view.window.rootViewController = navVC;

}else{
gouwuchelistViewController *vc = [[gouwuchelistViewController alloc]init];
vc.hidesBottomBarWhenPushed = YES;
[self.navigationController pushViewController:vc animated:YES];
}
}
#pragma mark: getter method EVNCustomSearchBar
- (EVNCustomSearchBar *)searchBar
{
    if (!_searchBar)
    {
        _searchBar = [[EVNCustomSearchBar alloc] initWithFrame:CGRectMake(KScreenWidth/2-(KScreenWidth-100)/2, 0, KScreenWidth-100, 30)];
//0a62ea
        _searchBar.backgroundColor = [UIColor clearColor]; // 清空searchBar的背景色
                _searchBar.textFieldColor = [UIColor colorWithHexString:@"fbf2f2"alpha:.32];
                _searchBar.textColor = [UIColor whiteColor];
    [_searchBar.cancelButton setTitleColor:[UIColor whiteColor]forState:UIControlStateNormal];
//        _searchBar.iconImage = [UIImage imageNamed:@"EVNCustomSearchBar.bundle/searchImageBlue.png"];
    _searchBar.iconImage = [UIImage imageNamed:@"search"];

        _searchBar.iconAlign = EVNCustomSearchBarIconAlignCenter;
        [_searchBar setPlaceholder:NSLocalizedString(@"请输入关键字", nil)];  // 搜索框的占位符
        _searchBar.placeholderColor = [UIColor whiteColor];
        _searchBar.delegate = self; // 设置代理
        [_searchBar.textField.layer setMasksToBounds:YES];

[_searchBar.textField.layer setCornerRadius:15];//设置矩形四个圆角半径

        [_searchBar sizeToFit];
    }
    return _searchBar;
}
- (void)searchBarTextDidEndEditing:(EVNCustomSearchBar *)searchBar{

shangpinlistViewController *vc = [[shangpinlistViewController alloc]init];
if (searchBar.text.length>0){
vc.searchstring  = searchBar.text;
vc.hidesBottomBarWhenPushed = YES;
[self.navigationController pushViewController:vc animated:YES];
}
}
- (void)searchBarSearchButtonClicked:(EVNCustomSearchBar *)searchBar{

}



- (void)midaction :(UIButton *)button{
if (button.tag==200) {

threelevelViewController *vc = [[threelevelViewController alloc]init];
//    zhaocanpinViewController *vc = [[zhaocanpinViewController alloc]init];
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}
if (button.tag==201) {

zjyoupinViewController *vc = [[zjyoupinViewController alloc]init];
vc.hidesBottomBarWhenPushed = YES;
[self.navigationController pushViewController:vc animated:YES];
//if([[[NSUserDefaults standardUserDefaults]objectForKey:@"autoLogin"]isEqualToString:@"Y"]||[[DanLi sharedDanLi].stay isEqualToString:@"yes"]){
//   rongziViewController *vc = [[rongziViewController alloc]init];
//    vc.type = @"main";
//    vc.hidesBottomBarWhenPushed = YES;
//    [self.navigationController pushViewController:vc animated:YES];
//
//
//}else{
//    UIStoryboard * storyBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
//
//               OALoginViewController * loginVC = [storyBoard instantiateViewControllerWithIdentifier:@"LoginVC"];
//        UINavigationController * navVC = [[UINavigationController alloc]initWithRootViewController:loginVC];
//        loginVC.navigationController.navigationBar.hidden = YES;
//                 self.view.window.rootViewController = navVC;
//}
}
if (button.tag==202) {
//zhaozcViewController *vc = [[zhaozcViewController alloc]init];
caigoulistViewController *vc = [[caigoulistViewController alloc]init];
    vc.hidesBottomBarWhenPushed = YES;
//    vc.id = @"7";
//    vc.titlename = @"找政策";
    [self.navigationController pushViewController:vc animated:YES];

}
if (button.tag==203) {
NSArray *array = _dic[@"themeList"];
NSMutableArray *muarray = [NSMutableArray array];

                              UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleAlert];
    
   for (NSDictionary *dic in array) {
     UIAlertAction *action= [UIAlertAction actionWithTitle:dic[@"name"] style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
zhutiViewController *vc = [[zhutiViewController alloc]init];
vc.type = dic[@"template"];
vc.idstring = dic[@"id"];
  [[NSUserDefaults standardUserDefaults] setObject:dic[@"id"] forKey:@"website"];

[self.navigationController pushViewController:vc animated:YES];

            }];
  [alert addAction:action];

}
    
           UIAlertAction *action= [UIAlertAction actionWithTitle:NSLocalizedString(@"关闭", nil) style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {


            }];
  [alert addAction:action];
            [self presentViewController:alert animated:YES completion:nil];
                  }
}



- (void)rightButtonAction:(UIButton *)button {

 UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:nil message:NSLocalizedString(@"是否退出系统", nil) delegate:self cancelButtonTitle:NSLocalizedString(@"取消", nil) otherButtonTitles:NSLocalizedString(@"控件-确定", nil), nil];
    [alertView show];
//    NSLocalizedString(@"是否退出系统", nil)
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{

if (buttonIndex == 1){

       UIStoryboard * storyBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];

 OALoginViewController * loginVC =  [storyBoard instantiateViewControllerWithIdentifier:@"LoginVC"];
     [[NSUserDefaults standardUserDefaults] setObject:@"N" forKey:@"autoLogin"];

          DanLi *danli = [[DanLi alloc] init];
    danli.stay = @"no";
    self.view.window.rootViewController = loginVC;


}
}
@end
